﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudentMS
{
    public partial class AddClassForm : Form
    {
        public AddClassForm()
        {
            InitializeComponent();
        }

        private void AddClassForm_Load(object sender, EventArgs e)
        {
            InitGradeList();
        }

        private void InitGradeList()
        {
            string sql = "select GradeId,GradeName from GradeInfo";
            DataTable dataTable = SqlHelper.GetDataTable(sql);
            cboGrades.DataSource = dataTable;
            cboGrades.DisplayMember = "GradeName";
            cboGrades.ValueMember = "GradeId";
            cboGrades.SelectedIndex = 0;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string className = txtClassName.Text.Trim();
            int gradeId = (int)cboGrades.SelectedValue;
            string remark = txtRemark.Text.Trim();
            if (string.IsNullOrEmpty(className))
            {
                MessageBox.Show("班级名称不能为空！", "添加班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            {
                string sqlExit = "select count(1) from ClassInfo where ClassName=@ClassName and GradeId=@GradeId";
                SqlParameter[] paras =
                {
                    new SqlParameter("@ClassName", className),
                    new SqlParameter("@GradeId", gradeId)
                };
                object objCount = SqlHelper.ExecuteScalar(sqlExit, paras);
                if (objCount == null || objCount == DBNull.Value || ((int)objCount) == 0)
                {
                    string sqlAdd = "insert into ClassInfo(ClassName,GradeId,Remark) values(@ClassName,@GradeId,@Remark)";
                    SqlParameter[] parasAdd =
                    {
                        new SqlParameter("@ClassName", className),
                        new SqlParameter("GradeId", gradeId),
                        new SqlParameter("@Remark", remark)
                    };
                    int count = SqlHelper.ExecuteNonQuery(sqlAdd, parasAdd);
                    if (count > 0)
                    {
                        MessageBox.Show($"班级: {className} 添加成功！", "添加班级提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("班级添加失败！", "添加班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        
                    }
                }
                else
                {
                    MessageBox.Show("班级名称已存在！", "添加班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
